//
//  ParentInfo.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-6.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "ParentInfo.h"
#import "NSString+PinYin.h"

@implementation ParentInfo

-(instancetype)initWithDictionary:(NSDictionary *)data{
    self = [super init];
    if (self) {
        _id = data[@"id"];
        _name = data[@"name"];
        _phoneNumber = data[@"phoneNumber"];
        _photo = [NSURL URLWithString:data[@"photo"]];
    }
    
    return self;
}

-(instancetype)initWithMessage:(EMConversation *)message{
    self = [super init];
    if (self) {
        _messages = message;
        _id = message.chatter;
    }
    return self;
}

+(NSArray *)parentsWithDictionaryArray:(NSArray *)arrayList{
    NSMutableArray *result = [[NSMutableArray alloc]initWithCapacity:[arrayList count]];
    for (NSDictionary *data in arrayList) {
        [result addObject:[[ParentInfo alloc]initWithDictionary:data]];
    }
    
    return result;
}

+(NSDictionary *)sortAndIndexWithPinyinLetter:(NSArray *)parentList{
    UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
    
    for (ParentInfo *data in parentList) {
        NSInteger sect = [theCollation sectionForObject:data collationStringSelector:@selector(getFirstName)];

        data.sectionNumber = sect;
    }
    
    NSInteger highSection = [[theCollation sectionTitles] count];
    
    NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
    for (int i=0; i<=highSection; i++) {
        NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
        [sectionArrays addObject:sectionArray];
    }
    for (ParentInfo *data in parentList) {
        [[sectionArrays objectAtIndex:data.sectionNumber] addObject:data];
    }
    
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:[sectionArrays count]];
    NSMutableArray *sectionIndexArray = [NSMutableArray array];
    
    for (int i = 0; i< [sectionArrays count]; i++) {
        NSMutableArray *sectionArray = [sectionArrays objectAtIndex:i];
        if ([sectionArray count] != 0) {
            NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(getFirstName)];
            [result addObject:sortedSection];
            [sectionIndexArray addObject:[[theCollation sectionTitles] objectAtIndex:i]];
        }
    }
    
    return @{@"data":result, @"index":sectionIndexArray};
    
}

+(NSArray *)updateParentListWithArray:(NSArray *)data parents:(NSArray *)parents{
    NSMutableArray *result = [NSMutableArray arrayWithArray:parents];
    for (ParentInfo *parent in result) {
        for (NSDictionary * dic in data) {
            if ([parent.id isEqualToString:dic[@"id"]]) {
                parent.name = dic[@"name"];
                parent.photo = dic[@"photo"];
            }
        }
        
    }
    return result;
}

- (NSString *) getFirstName {
    if ([_name canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语
        return _name;
    }
    else { //如果是非英语
        return [_name getFirstLetter];
    }
    
}
@end
